layui.use(['form', 'layedit', 'laydate', 'jquery'], function () {
    var form = layui.form
        , layer = layui.layer
        , $ = layui.jquery

    /**
     * 判断是否是数值
     * @param str 变量
     * @returns {boolean} 是返回true 不是返回false
     */
    function isNumber(str) {
        var n = Number(str);
        return !isNaN(n);
    }
    /**
     得到地址栏的参数
     */
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    let id = getQueryString("wageId");
    /**
     *当点击父页面的修改按钮时,会传入一个id到form.html页面
     *给form表单自动赋值
     */
    let wage = null;
    if (id != null) {//如果有值id不为空
        let url = "/logisticsWage/selectOne/" + id;
        $.ajax({
            type: "get",
            url: url,
            dataType: "json",
            success: function (resp) {
                if (resp.state === 1) {
                    wage = resp.data;
                    // $("#typeName").val(map.data.typeName);
                    form.val('exa', resp.data);//给子页面赋值
                }
            }
        });
    }
    //input框离失去焦点事件
    $(".layui-input-block input").on('blur', function () {
        if (!isNumber(this.value)){
            layer.msg('<p style="font-size: 15px"><i class="layui-icon layui-icon-face-cry" style="font-size: 30px; color: #ff0000;margin-top: 5px"></i> 请输入正确的数值</p>');
        }
    });
    //监听提交
    form.on('submit(sub)', function (data) {
        let data1 = data.field;
        let wageElse =data1.wageElse;
        let wageAllowance = data1.wageAllowance;
        let wageOvertime = data1.wageOvertime;
        if (wageElse == null||wageElse === " "||wageElse === ""){
            wageElse = 0.00;
        }
        if (wageOvertime == null||wageOvertime === " "||wageOvertime === ""){
            wageOvertime = 0.00;
        }
        if (wageAllowance == null||wageAllowance === " "||wageAllowance === ""){
            wageAllowance = 0.00;
        }
        //处理数字框
        if (isNumber(wageElse)&&isNumber(wageAllowance)&&isNumber(data1.wageOvertime)) {//如果是数值
            //如果输入为正确的数值
            if ($("#wageElse").val() < 100000000&&$("#wageOvertime").val() < 100000000&&$("#wageAllowance").val() < 100000000){
                //处理返回的数据
                let sum = Number(wage.wageBasepay)+Number(wage.wagePost)+Number(wageOvertime)+Number(wageAllowance);
                let sums = sum-(sum*0.222)-Number(wageElse)+1;
                console.info(sums+" "+sum);
                if (sums < 100000000){
                    let dataWage = {
                        wageId: id
                        , wageElse: wageElse
                        , wageOvertime: wageOvertime
                        , wageAllowance: wageAllowance
                        , wageRemark: data1.wageRemark
                        , wageBasepay: wage.wageBasepay
                        , wagePost: wage.wagePost
                    };
                    $.ajax({
                        type: "get",
                        url: '/logisticsWage/addOrUpdate',
                        data: dataWage,
                        dataType: "json",
                        success: function (map) {
                            // 注意：parent 是 JS 自带的全局对象，可用于操作父页面
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            if (map.state === 1) {
                                parent.layer.close(index);
                                parent.table.reload('WageList');
                                parent.layer.msg(map.message);
                            } else {
                                parent.layer.close(index);
                                layer.msg(map.message);
                            }
                        }
                    });
                }else {
                    layer.msg('<p style="font-size: 15px"><i class="layui-icon layui-icon-face-cry" style="font-size: 30px; color: #ff0000;margin-top: 5px"></i> 本月总工资数据过大！</p>');
                }
            }else {
                layer.msg('<p style="font-size: 15px"><i class="layui-icon layui-icon-face-cry" style="font-size: 30px; color: #ff0000;margin-top: 5px"></i> 填写的数据过大！</p>');
            }
        }else{
            layer.msg('<p style="font-size: 15px"><i class="layui-icon layui-icon-face-cry" style="font-size: 30px; color: #ff0000;margin-top: 5px"></i> 请输入正确的数值！</p>');
        }
        return false;
    });
    //监听返回
    $("#return").on('click', function () {
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    });
});
